import { PageConfig } from 'next';
import anylogger from 'anylogger';
import '@mmstudio/an000042';
import an48 from '@mmstudio/an000048';
import { tbcourseassignment, tbjob } from '../../../../atoms/db';

const logger = anylogger('pages/api/zllweapp/workquery/s001.api');
export type Idata = Pick<ITbcourseassignment, 'coursename' | 'jobtitle' | 'jobcontent' | 'date'>[]

export type Result = {
	ok: true;
	data: Idata
	data1: Pick<ITbcourseassignment, 'job_id' | 'coursename' | 'jobtitle' | 'jobcontent' | 'date'>[]
	data2: Pick<ITbcourseassignment, 'coursename' | 'jobtitle' | 'jobcontent' | 'date' | 'job_id'>[]
} | {
	ok: false;
	message: string;
};

export type Message = {

}

/**
 * pages/api/zllweapp/workquery/s001.api
 */
const handler = an48<Result>();

handler.post(async (req, res) => {
	try {
		logger.debug('msg body:', req.body);
		//**通过后期获取登陆学生学号 */

		const student_id = '201942908581';
		//**取出当前登录学生的所有作业id */

		const job_id = await tbjob().select('job_id').where({
			student_id: student_id
		});
		//**已批阅的作业id */
		const job_id1 = await tbjob().select('job_id').where({
			approvedlogo: 1,
			student_id: student_id
		});

		//**未提交的作业id */
		const job_id2 = await tbjob().select('job_id').where({
			s_jobcontext: null,
			student_id: student_id
		});
		//**已完成的作业id */
		const job_id3 = await tbjob().select('job_id').where({

			student_id: student_id
		});
		const Job_id = job_id.map((it) => {
			return it.job_id;
		});
		const Job_id1 = job_id1.map((it) => {
			return it.job_id;
		});
		const Job_id2 = job_id2.map((it) => {
			return it.job_id;
		});
		//**当前学生的所有作业 */
		const data = await tbcourseassignment().select('coursename', 'jobtitle', 'jobcontent', 'date').whereIn('job_id', Job_id);
		//**已批阅的作业 */
		const data1 = await tbcourseassignment().select('coursename', 'jobtitle', 'jobcontent', 'date', 'job_id').whereIn('job_id', Job_id1);
		//**未提交作业 */
		const data2 = await tbcourseassignment().select('coursename', 'jobtitle', 'jobcontent', 'date', 'job_id').whereIn('job_id', Job_id2);




		res.status(200).json({ ok: true, data, data1, data2 });
	} catch (error) {
		logger.trace(error);
		res.status(200).json({ ok: false, message: (error as Error).message });
	}
});

export const config = {} as PageConfig;

export default handler;
